﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Project1
{
    public class RadarDraw
    {
        private Texture2D mPixel;

        private Texture2D mRectPixel;

        private ArrayList mPixelArray;

        public Color mDefaultColor;

        public Vector2 mPosition;


        public RadarDraw(GraphicsDevice aGraphics)
        {
            mPixel = new Texture2D(aGraphics, 1, 1, false, SurfaceFormat.Color);

            mRectPixel = new Texture2D(aGraphics, 100, 100, false, SurfaceFormat.Color);
            Color[] lPixels = new Color[1];
            lPixels[0] = Color.White;
            mPixel.SetData<Color>(lPixels);

            mDefaultColor = Color.White;
            mPosition = new Vector2(974, 718);

            mPixelArray = new ArrayList();            

        }

        public void Clear()
        {
            mPixelArray.Clear();
        }

        public void Render(SpriteBatch aSpriteBatch)
        {
            if (mPixelArray.Count < 2)
                return;

            for (int i = 1; i < mPixelArray.Count; i++)
            {
                if (i % 101 == 0)
                {
                    continue;
                }
                Vector2 lVector1 = (Vector2)mPixelArray[i - 1];
                Vector2 lVector2 = (Vector2)mPixelArray[i];
                
                // Calculate the distance between the 2 vectors
                float lDistance = Vector2.Distance(lVector1, lVector2);
                float lAngle = (float)Math.Atan2((double)(lVector2.Y - lVector1.Y),
                                                 (double)(lVector2.X - lVector1.X));
                
                // Strech the pixel between the 2 vectors
                aSpriteBatch.Draw(mPixel, mPosition + lVector1,
                    null, mDefaultColor,
                    lAngle,
                    Vector2.Zero,
                    new Vector2(lDistance, 1),
                    SpriteEffects.None,
                    0);
            }
        }

        public void CreateCircle(float aRadius, int aSides)
        {
            float lMax = (float)Math.PI * 2;
            float lStep = lMax / (float)aSides;

            for (float theta = 0; theta < lMax; theta += lStep)
            {
                mPixelArray.Add(new Vector2(aRadius * (float)Math.Cos((double)theta),
                                            aRadius * (float)Math.Sin((double)theta)));
            }
            // then add the first vector again so it's a complete loop
            mPixelArray.Add(new Vector2(aRadius * (float)Math.Cos(0),
                    aRadius * (float)Math.Sin(0)));
        }

        public void CreateRadar(SpriteBatch aSpriteBatch)
        {
            CreateCircle(50, 100);
            CreateCircle(25, 100);
            CreateCircle(37, 100);

            Render(aSpriteBatch);
        }

        public void DrawX(SpriteBatch aSpriteBatch, SpriteFont aSpriteFont, Vector2 lPosition)
        {
            lPosition *= 50;
            string lTemp = 'x'.ToString();
            aSpriteBatch.DrawString(aSpriteFont, lTemp, lPosition + mPosition, Color.Red); 
        }
    }
}
